package com.css.apps.base.user.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import com.css.db.query.QueryCache;
import com.css.db.util.StringHelper;
import com.css.ids.server.service.IdsService;
import com.css.ids.server.service.dao.IdsOrgDao;

public class UserCountGraphService {

	private transient UserCountGraphDao dao = new UserCountGraphDao();
	
	public List<Object> getHeaderRegistUserCountTabData(int year) {
		List<String> orgIds = IdsOrgDao.listAllOrgId(1, null, IdsService.ALL_ORG_TYPE);
		List<Object> res = dao.getRegistUserCount(year, orgIds);
		Collections.sort(res, new Comparator<Object>() {
			@Override
			public int compare(Object o1, Object o2) {
				Object[] obj1 = (Object[]) o1, obj2 = (Object[]) o2;
				if (obj1 != null && obj2 != null) {
					String month1 = obj1[0] + "", month2 = obj2[0] + "";
					if (StringHelper.isNotEmpty(month1, month2)) {
						int m1 = Integer.parseInt(month1), m2 = Integer.parseInt(month2);
						if (m1 > m2) {
							return 1;
						}
						if (m1 == m2) {
							return 0;
						}
					}
				}
				return -1;
			}
		});
		return res;
	}
	
	public List<Object> getSubRegistUserCountTabData(int year) {
		List<String> orgIds = IdsOrgDao.listAllOrgId(2, null, IdsService.ALL_ORG_TYPE);
		return dao.getRegistUserCount(year, orgIds);
	}
	
	public List<Object[]> getAuthSysUserCountTabData() {
		List<Object[]> tabData = new ArrayList<>();
		double allUserSum = Double.valueOf(dao.getAllUserSum());
		Map<String, String> sysInfo = dao.getAllSysNameIdMap();
		List<Object[]> userCount = dao.getUserCountBySys();
		for (int i = 0; i < userCount.size(); i++) {
			Object[] uc = userCount.get(i);
			Object[] row = new Object[3];
			row[0] = sysInfo.get(uc[0]); // 系统名称
			row[1] = uc[1]; // 使用当前系统的人数
			double sum = Double.valueOf(String.valueOf(uc[1]));
			String res = String.valueOf(sum / (allUserSum) * 100);
			int dotIdx = res.indexOf(".");
			String pre = res.substring(0, dotIdx);
			row[2] = dotIdx == -1 ? "0" : pre + res.substring(dotIdx, dotIdx + 3);
			tabData.add(row);
		}
		return tabData;
	}
	
	public String[] getAuthSysUserCountChartData() {
		String[] res = new String[2];
		List<Object[]> tabData = getAuthSysUserCountTabData();
		StringBuilder sbX = new StringBuilder(), sbY = new StringBuilder();
		for (int i = 0; i < tabData.size(); i++) {
			Object[] uc = tabData.get(i);
			sbX.append("'").append(uc[0]).append("'").append(",");
			sbY.append(uc[1]).append(",");
		}
		res[0] = dislodgeComma(sbX);
		res[1] = dislodgeComma(sbY);
		return res;
	}
	
	@SuppressWarnings("unchecked")
	public List<Object[]> getHeaderUserStatusCountTabData() {
		List<String> headerOrgIds = IdsOrgDao.listAllOrgId(1, null, IdsService.ALL_ORG_TYPE);
		StringBuilder hql = new StringBuilder("select count(uuid), openFlag from SUser");
		hql.append(" where userType = '1' and uuid in (select userId from SUserOrg where orgId in (:orgIds))")
			.append(" group by openFlag");
		List<Object[]> res = new QueryCache(hql.toString()).setParameter("orgIds", headerOrgIds).list(); 
		return res;
	}
	
	@SuppressWarnings("unchecked")
	public List<Object[]> getSubOrgUserStatusCountTabData() {
		List<String> headerOrgIds = IdsOrgDao.listAllOrgId(1, null, IdsService.ALL_ORG_TYPE);
		StringBuilder hql = new StringBuilder("select count(uuid), openFlag from SUser");
		hql.append(" where userType = '1' and uuid in (select userId from SUserOrg where orgId not in (:orgIds))")
			.append(" group by openFlag");
		List<Object[]> res = new QueryCache(hql.toString()).setParameter("orgIds", headerOrgIds).list(); 
		return res;
	}
	/**
	 * 擦除逗号
	 */
	private String dislodgeComma(CharSequence cs) {
		String res = null;
		if(cs instanceof StringBuilder || cs instanceof String) {
			res = cs.toString();
		} 
		if(StringHelper.isEmpty(res)) {
			return "";
		}
		if(res.endsWith(",")) {
			res = res.substring(0, res.length() - 1);
		}
		return res;
	}
}
